#! /usr/bin/env python

"""
Used in:
"""

import os
import tempfile
import shutil
from math import *

## Writes the header of a pspicture LaTeX document.
#
def writeheader(paperheight,paperwidth,xoffset,yoffset,scale):
    out = ""
    out += "\\documentclass{article}\n"
    out += "\\usepackage{pstricks}\n"
    out += "\\usepackage{pst-all}\n"
    out += "\\selectcolormodel{rgb}\n"
    out += "\\usepackage{pspicture}\n"
    out += "\\usepackage[dvips,paperwidth=%10.5fcm,paperheight=%10.5fcm,\n" % (paperwidth,paperheight)
    out += "left=0cm,right=0cm,top=0cm,bottom=0cm,headheight=0pt,headsep=0pt,footskip=0pt,\n"
    out += "]{geometry}\n"
    out += "\\oddsidemargin=-1in\n"
    out += "\\topmargin=-1in\n"
    out += "\\parindent=0pt\n"
    out += "\\begin{document}\n"
    out += "\\pagestyle{empty}\n"
    out += "\\SpecialCoor\n"
    out += "\\psset{xunit=%10.5fcm}\n" % (1./scale)
    out += "\\psset{yunit=%10.5fcm}\n" % (1./scale)
    out += "\\psset{runit=%10.5fcm}\n" % (1./scale)
    out += "\\begin{pspicture}(0,0)(-%10.5f,%10.5f)\n" % (xoffset*scale,(paperheight-yoffset)*scale)
    out += "\\linethickness{0.8pt}\n"
    return out 

## Writes the footer for a pspicture LaTeX file.
#
def writefooter():
    out = ""
    out += "\\end{pspicture}\n"
    out += "\\end{document}\n"
    return out

myfilename = 'rho_R_comp'

R = 4.0 # cm
rho = 1.0 #cm

# Figure properties
width = 12.16 # cm
height = width
xoffset = 0.5 * (width - R)
yoffset = 0.5 * (height - R)

## Current working directory.
cwd = os.getcwd()
## Temporary directory for files used in figure construction.
tempdir = tempfile.mkdtemp('.drawtracker')
## Path to the .tex file
texpath = os.path.join(tempdir, myfilename+'.tex')

## The Tex file to which the rho-phi figure is written.
texfile = open(texpath,'w')

# Write the LaTeX document headers.
texfile.write(writeheader(height,width,xoffset,yoffset,1.0))

texfile.write('\\pscircle[linewidth=0.3pt](%10.5f,%10.5f){%10.5f}\n' % (0.0, 0.0,  rho))
texfile.write('\\pscircle[linewidth=0.3pt,linestyle=dashed,dash=1.5pt 0.75pt](%10.5f,%10.5f){%10.5f}\n' % (0.0, 0.0,  R  ))
texfile.write('\\pscircle[linewidth=0.3pt](%10.5f,%10.5f){%10.5f}\n' % (R,   0.0,  rho))
texfile.write('\\pscircle[linewidth=0.3pt,linestyle=dashed,dash=1.5pt 0.75pt](%10.5f,%10.5f){%10.5f}\n' % (R,   0.0,  R  ))
texfile.write('\\pscircle[linewidth=0.3pt](%10.5f,%10.5f){%10.5f}\n' % (R,   R  ,  rho))
texfile.write('\\pscircle[linewidth=0.3pt,linestyle=dashed,dash=1.5pt 0.75pt](%10.5f,%10.5f){%10.5f}\n' % (R,   R  ,  R  ))
texfile.write('\\pscircle[linewidth=0.3pt](%10.5f,%10.5f){%10.5f}\n' % (0.0, R  ,  rho))
texfile.write('\\pscircle[linewidth=0.3pt,linestyle=dashed,dash=1.5pt 0.75pt](%10.5f,%10.5f){%10.5f}\n' % (0.0, R  ,  R  ))

texfile.write('\\psline[linewidth=0.3pt](%10.5f,%10.5f)(%10.5f,%10.5f)\n' % (0.0,R,0.0,0.0))

## Angle travelled through along the circle
psi = asin(rho/(2.*R))
psi_deg = 180. * psi / pi
print "Angle travelled through = %10.5f" % psi
texfile.write('\\psline[linewidth=0.3pt](%10.5f,%10.5f)(%10.5f;%10.5f)\n' % (0.0,0.0,R,psi_deg))

texfile.write('\\psline[linewidth=0.3pt](%10.5f,%10.5f)(%10.5f,%10.5f)\n' % (R,0.0,R,R))
texfile.write('\\psline[linewidth=0.3pt](%10.5f,%10.5f)(%10.5f,%10.5f)\n' % (0.0,R,R,R))
texfile.write('\\psline[linewidth=0.3pt](%10.5f,%10.5f)(%10.5f,%10.5f)\n' % (0.0,0.0,R,0.0))

texfile.write('\\psline[linewidth=0.3pt](%10.5f,%10.5f)(%10.5f,%10.5f)\n' % (R,0.0,R*(1 - sin(psi)),R*cos(psi)))
texfile.write('\\psline[linewidth=0.3pt](%10.5f,%10.5f)(%10.5f,%10.5f)\n' % (R,R,R*(1 - cos(psi)),R*(1-sin(psi))))
texfile.write('\\psline[linewidth=0.3pt](%10.5f,%10.5f)(%10.5f,%10.5f)\n' % (0.0,R,R*sin(psi),R*(1 - cos(psi))))

# Write the LaTeX document footer.
texfile.write(writefooter())
# Close the LaTeX file.
texfile.close()

#Make the PDF from the latex.
import subprocess

#run LaTeX on the generated figure LaTeX file.
#print texpath
#texcmd = ["latex", texpath]

##filenames = [rhophifilename, rhozfilename, mapfilename]
filenames = [myfilename]
for filename in filenames:
    ## Process for running the LaTeX figure generation.
    texproc = subprocess.Popen(["latex", "-interaction=batchmode", os.path.join(tempdir,filename+".tex")], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=tempdir)
    texproc.wait()
    
    ## Process for running the dvi to ps conversion.
    dvproc = subprocess.Popen(["dvips", "-q", os.path.join(tempdir,filename+".dvi")], stdout=subprocess.PIPE, cwd=tempdir)
    dvproc.wait()
    
    ## Process for running the ps to pdf conversion.
    cnvproc = subprocess.Popen(["ps2pdf", os.path.join(tempdir,filename+".ps")], stdout=subprocess.PIPE, cwd=tempdir)
    cnvproc.wait()
    
    ## The output path for the final PDF.
    outpath = os.path.join(tempdir, filename+".pdf")
    if os.path.exists(outpath):
        shutil.copy(outpath, cwd)
    ## The output path for the final .tex file (for debugging).
    outtex = os.path.join(tempdir, filename+".tex")
    if os.path.exists(outtex):
        shutil.copy(outtex, cwd)

shutil.rmtree(tempdir, ignore_errors=True) # delete the temporary files
